package com.tedu.openmedical.medicalsystem.UserRole.service.impl;

import com.tedu.openmedical.medicalsystem.UserRole.mapper.UserRoleMapper;
import com.tedu.openmedical.medicalsystem.UserRole.pojo.dto.RoleUserSaveParam;
import com.tedu.openmedical.medicalsystem.UserRole.pojo.entity.UserRole;
import com.tedu.openmedical.medicalsystem.UserRole.pojo.vo.UserRoleVO;
import com.tedu.openmedical.medicalsystem.UserRole.service.UserRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<UserRoleVO> selectUserRole(String roleType) {
        return userRoleMapper.selectUserRole(roleType);
    }

    @Override
    public void deleteUserRole(Long roleId) {
        UserRoleVO userRoleVO = userRoleMapper.selectUserRoleById(roleId);
        if (userRoleVO!= null && !"管理员".equals(userRoleVO.getRoleName()) && !"超级管理员".equals(userRoleVO.getRoleName())){
            userRoleMapper.deleteUserRole(roleId);
        }
    }

    @Override
    public void addRoleUser(RoleUserSaveParam roleUserSaveParam) {
        UserRole userRole = new UserRole();
        BeanUtils.copyProperties(roleUserSaveParam, userRole);
        //判断是否新增角色
        if(roleUserSaveParam.getRoleId() == null){
            userRole.setRoleCode("role_" + roleUserSaveParam.getRoleName());
            userRoleMapper.insertUserRole(userRole);
        }
    }
}
